import os
import pandas as pd
import re
import math
def get_data(file_path):
    # 读取Excel文件
    df = pd.read_excel(file_path)

    return df
def delete_extra_zero(num):
    fractional, integer = math.modf(num)
    if fractional == 0:
        num = int(integer)
    else:
        num = num
    return num
def product_information_get(path):
    df = pd.read_excel(path, sheet_name=0, header=None,skiprows=19, nrows=13)
    # print(df)
    product_information={
        "FrCenterTest":[],#基频中心频率
        "FundSweptBandwidth":[],#基频扫频带宽
        "FundFreqOutputPower":[],#基频输出功率
        "FundFreqCenter":[], #测试中心频率（Hz）
        "FundFreqLimits":[], #驱动功率、驱动功率单位
        "基频频率结果调整":[], #驱动功率、驱动功率单位
        "PowerAppliedRR":[],#驱动电阻值
        "泛音中心频率":[],#删除该字段
        "泛音扫频带宽":[],#删除该字段
        "泛音输出功率":[],#负载
        "泛音规格中心":[], #删除该字段
        "泛音规格上下限":[],#测试模式
        "泛音频率结果调整": [],
        "CreateDate":[]#创建日期

    }
    print(path)
    # print(df)
    FrCenterTest=df.iloc[0, 0]
    print(FrCenterTest)
    FrCenterTest_pattern = re.compile(r"基频中心频率:\s(\d*.\d*)(\w*)")
    FrCenterTest_result = re.search(FrCenterTest_pattern, FrCenterTest)
    if FrCenterTest_result:
        number=float(FrCenterTest_result.group(1))
        product_information["FrCenterTest"]=str(delete_extra_zero(number))+FrCenterTest_result.group(2)
        print(product_information["FrCenterTest"])
        print("基频中心频率信息获取:"+str(product_information["FrCenterTest"]))
    #FundSweptBandwidth-Fundamental swept bandwidth基频扫频带宽
    FundSweptBandwidth=df.iloc[1, 0]
    FundSweptBandwidth_pattern = re.compile(r"基频扫频带宽:\s(\d*)(\w*)")
    FundSweptBandwidth_result = re.search(FundSweptBandwidth_pattern, FundSweptBandwidth)
    if FundSweptBandwidth_result:
        number=float(FundSweptBandwidth_result.group(1))
        product_information["FundSweptBandwidth"] = str(delete_extra_zero(number)) + FundSweptBandwidth_result.group(2)
        print("基频扫频带宽信息获取:"+str(product_information["FundSweptBandwidth"]))
    #FundFreqOutputPower-基频输出功率Fundamental frequency output power基频输出功率: 50%
    FundFreqOutputPower=df.iloc[2, 0]
    FundFreqOutputPower_pattern = re.compile(r"基频输出功率:\s*(\d*%)")
    FundFreqOutputPower_result = re.search(FundFreqOutputPower_pattern, FundFreqOutputPower)
    if FundFreqOutputPower_result:
        product_information["FundFreqOutputPower"] = FundFreqOutputPower_result.group(1)
        print("基频输出功率信息获取:"+str(product_information["FundFreqOutputPower"]))
    FundFreqCenter=df.iloc[3, 0]
    FundFreqCenter_pattern = re.compile(r"基频规格中心:\s*(\d*.\d*)(\w*)")
    FundFreqCenter_result = re.search(FundFreqCenter_pattern, FundFreqCenter)
    if FundFreqCenter_result:
        product_information["FundFreqCenter"] = FundFreqCenter_result.group(1)+FundFreqCenter_result.group(2)
        print("基频规格中心信息获取:" + str(product_information["FundFreqCenter"]))
    FundFreqLimits=df.iloc[4,0]
    FundFreqLimits_pattern=re.compile(r"基频规格上下限:\s*(±\d*.\d*\w*)")
    FundFreqLimits_result=re.search(FundFreqLimits_pattern,FundFreqLimits)
    if FundFreqLimits_result:
        product_information["FundFreqLimits"]=FundFreqLimits_result.group(1)
        print("基频规格上下限信息获取:" + str(product_information["FundFreqLimits"]))

if __name__ == "__main__":
    file_path = r'in/MT-01/101_频率及△F测试_MT-01_3840692_SJ23050865-SZ_24585-24590_Ethan_2024-03-20-15-46-18.xls'
    # data = get_data(file_path)
    product_information_get(file_path)

    # 打印data和year_month
    # print(data)



# 可以将data和year_month用于后续处理
